package controllers

import (
	"lottery/models"
	"encoding/json"
	"github.com/astaxie/beego"
	"lottery/service"
	"fmt"
	"time"
)

// 下注控制器
type BetController struct {
	beego.Controller
	 service.Bet
}

// @Title Create
// @Description 下注
// @Param	body		body 	models.Bet	true		"The bet arrays"
// @Success 200 {string} models.Bet.Id
// @Failure 403 body is empty
// @router / [post]
func (ctrl *BetController) Post() {
	var  bet []*models.Bet
	json.Unmarshal(ctrl.Ctx.Input.RequestBody, &bet)
	// mailId int64
	//make test data
	start:=time.Now().UnixNano()
	fmt.Println(start)

	bet=service.MakeBet()
	status,err := ctrl.Bet.AddItem(bet)

	end:=time.Now().UnixNano()
	fmt.Println(end)
	fmt.Println(end-start)
	var rsp models.Rsp
	if err!=nil {
		rsp=models.Rsp{Status:status,Error:err.Error()}
	}else{
		rsp=models.Rsp{Error:"ok"}
		fmt.Println(rsp)
	}
	ctrl.Data["json"] =rsp
	ctrl.ServeJSON()
}


// @Title Get
// @Description find bet by betId
// @Param	betId		path 	string	true		"the betId you want to get"
// @Success 200 {object} models.Bet
// @Failure 403 :betId is empty
// @router /:betId [get]
func (ctrl *BetController) Get() {
	var betId int64;
	ctrl.Ctx.Input.Bind(&betId,":betId")
	fmt.Println(betId)
	bet,err := ctrl.Bet.GetItemById(betId)
	var rsp models.Rsp
	if err!=nil {
		rsp=models.Rsp{Status:-1,Error:err.Error()}
	}else{
		rsp=models.Rsp{Data:bet}
	}
	ctrl.Data["json"] = rsp
	ctrl.ServeJSON()
}

// @Title GetAll
// @Description get all mail by page
// @Param	page		query 	string	true		"the page you want to get"
// @Param	size		query 	string	true		"the size you want to get"
// @Param	userId		query 	string	false	 	"the size you want to get"
// @Success 200 {object} models.Bet
// @Failure 403 :pageSize is empty
// @router / [get]
func (ctrl *BetController) GetAll() {
	var  page int
	var  size int
	var userId int64
	ctrl.Ctx.Input.Bind(&page,"page")
	ctrl.Ctx.Input.Bind(&size,"size")
	ctrl.Ctx.Input.Bind(&userId,"userId")

	params := models.Bet{UserId:userId}
	//controller.Ctx.Input.Params()
	mailList,err := ctrl.Bet.GetItemByPage(&params,page,size)
	var rsp models.Rsp
	if err!=nil {
		rsp=models.Rsp{Status:-1,Error:err.Error()}
	}else{
		rsp=models.Rsp{Data:mailList}
	}
	ctrl.Data["json"] = rsp
	ctrl.ServeJSON()
}

// @Title Update
// @Description update the object
// @Param	betId		path 	string	true		"The mailId you want to update"
// @Param	body		body 	models.Mail	true		"The body"
// @Success 200 {object} models.Bet
// @Failure 403 :betId is empty
// @router /:betId [put]
func (ctrl *BetController) Put() {
	var betId int64
	ctrl.Ctx.Input.Bind(&betId,":betId")

	var bet models.Bet
	json.Unmarshal(ctrl.Ctx.Input.RequestBody, &bet)

	status,err := ctrl.Bet.UpdateItemById(betId,&bet)
	var rsp models.Rsp
	if err!=nil {
		rsp=models.Rsp{Status:status,Error:err.Error()}
	}else{
		rsp=models.Rsp{Data:"更新成功"}
	}
	ctrl.Data["json"] = rsp
	ctrl.ServeJSON()
}

// @Title Delete
// @Description delete the object
// @Param	betId		path 	string	true		"The mailId you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 mailId is empty
// @router /:betId [delete]
func (ctrl *BetController) Delete() {
	var betId int64
	ctrl.Ctx.Input.Bind(&betId,":betId")

	status,err := ctrl.Bet.DeleteItemById(betId)
	var rsp models.Rsp
	if err!=nil {
		rsp=models.Rsp{Status:status,Error:err.Error()}
	}else{
		rsp=models.Rsp{Data:"删除成功"}
	}
	ctrl.Data["json"] = rsp
	ctrl.ServeJSON()
}

